//
//  LYRegister2ViewController.m
//  artist
//
//  Created by Jay_Apple on 16/3/13.
//  Copyright © 2016年 viviyoo. All rights reserved.
//

#import "LYRegister2ViewController.h"
#import "NSString+textCheck.h"
#import "UIButton+CountDown.h"
#import "LYLoginInfoModel.h"
#import "LYStudentDataViewController.h"

@interface LYRegister2ViewController ()
@property (weak, nonatomic) IBOutlet UITextField *IDCode;

@property (weak, nonatomic) IBOutlet UITextField *pwd;

@property (weak, nonatomic) IBOutlet UIButton *commitBtn;

@end

@implementation LYRegister2ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setUpComponent];
}

- (void)setUpComponent{
    
    if (self.type == viewTypePwd) {

        self.navigationItem.title = @"找回密码";
        [self.commitBtn setTitle:@"找回密码" forState:UIControlStateNormal];
    }else{
        self.navigationItem.title = @"注册";
        [self.commitBtn setTitle:@"完成注册" forState:UIControlStateNormal];
    }
    
}

#pragma mark - 网络请求
/**
 *  注册
 */
- (void)sendRegister{

    [MBProgressHUD showMessage:@"正在提交"];
    
    [LYRequestTool registerWithUserName:self.phoneNum Password:self.pwd.text verify_code:self.IDCode.text success:^(NSString *token) {
        [MBProgressHUD hideHUD];
        [MBProgressHUD showMessage:@"注册成功,正在登录"];
        
        //自动登陆
        [LYRequestTool loginWithUserName:self.phoneNum Password:self.pwd.text success:^(LYLoginInfoModel *info) {
            
            //存到本地
            [VVYSaveDataManager saveDataWithData:info key:LYUserLoginInfo];
            
            //拿到token请求用户信息
            [LYRequestTool acquireUserInfoWithToken:info.token success:^(LYUserInfoModel *userInfo) {
                
                
                [MBProgressHUD hideHUD];
                [MBProgressHUD showSuccess:@"登录成功！"];
                
                //保存本地
                [VVYSaveDataManager saveDataWithData:userInfo key:KEY_USERMESSAGE];
                [[VVYCheckStatus sharedInstance] checkIsLogin];
                
                //弹出完善资料
                [self pushToMessageVC];
                
                [self.navigationController dismissViewControllerAnimated:YES completion:nil];
                
            } failure:^(NSString *code, NSString *message) {
                
                [MBProgressHUD hideHUD];
                [MBProgressHUD showError:message];
                
            }];
            
            
            
        } failure:^(NSString *code, NSString *message) {
            [MBProgressHUD hideHUD];
            [MBProgressHUD showError:message];
        }];
        
        
        
    } failure:^(NSString *code, NSString *message) {
        [MBProgressHUD hideHUD];
        [MBProgressHUD showError:message];
    }];

}

/**
 *  找回密码
 */
- (void)findPwd{

    [LYRequestTool resetPasswordWithUserName:self.phoneNum Password:self.pwd.text verify_code:self.IDCode.text success:^{
        
        [MBProgressHUD showSuccess:@"重置成功"];
        [self.navigationController popToRootViewControllerAnimated:YES];
        
    } failure:^(NSString *code, NSString *message) {
        [MBProgressHUD showError:message];
    }];

}


#pragma mark - 按钮点击方法

- (IBAction)registerBtnOnclick:(id)sender {
    
    
    if (!([self checkIDCode] && [self checkPwd])) {
        return;
    }
    
    switch (self.type) {
        case viewTypePwd:
            
            [self findPwd];
            
            break;
            
        case viewTypeRegister:
            
            [self sendRegister];
            
            break;
    }
    
    
}
- (IBAction)IDCodeBtnOnclick:(UIButton *)sender {
    
    [sender startTime:59 title:@"发送验证码" waitTittle:@"重新发送"];
    
    LYverifyType type = self.type ==  viewTypePwd ? verifyTypeFindPassword : verifyTypeVerify;
    
    [LYRequestTool sendMessageVerificationWithmobile_phone:self.phoneNum verify_type:type success:^(NSString *code) {
        
        [MBProgressHUD showOnlyText:@"验证码发送成功,请注意查收"];
    } failure:^(NSString *code, NSString *message) {
        [MBProgressHUD showError:message];
    }];

}


/**
 *  显示/隐藏密码
 */
- (IBAction)showHidePwd:(UIButton *)sender {
    
    //改变textfied模式
    sender.selected = !sender.selected;
    
    self.pwd.secureTextEntry = !sender.selected;
    
    
}


#pragma mark - 文本校验
- (BOOL)checkIDCode{

    if (self.IDCode.text.length == 0) {
        [MBProgressHUD showError:@"验证码不能为空"];
        return NO;
    }else if (![self.IDCode.text checkIndentityCode]){
        
        [MBProgressHUD showOnlyText:@"请输入正确的验证码"];
        return NO;
    }else{
        
        return YES;
    }
}

- (BOOL)checkPwd{

    if (self.pwd.text.length == 0) {
        [MBProgressHUD showError:@"密码不能为空"];
        return NO;
    }else if(![self.pwd.text checkPassword]){
        [MBProgressHUD showOnlyText:@"请输入正确格式密码"];
        return NO;
    }else{
        return YES;
    }
}

#pragma mark - 其他
- (void)pushToMessageVC{

    UITabBarController *tab = (UITabBarController *)LYKeyWindow.rootViewController;
    UINavigationController *nav = tab.selectedViewController;
    LYStudentDataViewController *messageVC = [[LYStudentDataViewController alloc] initWithCanEditIdentity:YES];
    
    [nav pushViewController:messageVC animated:NO];

}

@end
